package com.banzhi.shop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.adapter.MyAdapter;
import com.banzhi.shop.adapter.QuickAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.AvatarData;
import com.banzhi.shop.data.GroupShareBean;
import com.banzhi.shop.data.Icons;
import com.banzhi.shop.data.JudgeGroupStatusBean2;
import com.banzhi.shop.model.CallBack.GroupShareCallBack;
import com.banzhi.shop.model.CallBack.JudgeGroupStatusCallBack;
import com.banzhi.shop.model.Impl.GroupShareImpl;
import com.banzhi.shop.model.Impl.JudgeGroupStatusImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.CircleImageView;
import com.banzhi.shop.view.VpSwipeRefreshLayout;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.sharesdk.framework.ShareSDK;

import static com.banzhi.shop.MyApp.context;

//拼团订单详情(拼团)
public class GroupOrderDetailsActivity extends BaseActivity implements JudgeGroupStatusCallBack, View.OnClickListener, GroupShareCallBack, SwipeRefreshLayout.OnRefreshListener {

    private ImageView mIvback;
    private ImageView mIvThumb;
    private TextView mTvTitle;
    private TextView mTvGroupNum;
    private TextView mTvGroupsprice;
    private TextView mTvGroupProfit;
    private JudgeGroupStatusImpl mModel;
    private String mGoodid;
    private String mTeamid;
    private JudgeGroupStatusBean2 mJudgeGroupBean;
    private TextView mTvJoin;
    private ImageView mIvGrouping;
    private ImageView mIvGroupSucess;
    private ImageView mIvGroupError;
    private ImageView mIvOpenSuccess;
    private TextView mTvLack;
    private TextView mTvLack2;
    private TextView mTvHour;
    private TextView mTvMinute;
    private TextView mTvSecond;
/*    private int type = 1;*/

    private Timer timer;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            showTimeDate();
        }
    };
    private GroupShareImpl mGroupShareModel;
    private GroupShareBean mGroupShareBean;
    //  private WXshareImpl  wxmodel;
    /**
     * 用于存储该商品的
     */
    private String wxURL;
    private static final int PENGYOUQUAN = 1;
    private static final int WEIXINHAOYOU = 2;
    private static final int LIANJIE = 3;
    private static final int ERWEIMA = 4;

    /**
     * 点击选择规格你的确定时  来判断去做什么事情
     * 点击选择规格  type ==1；
     * 点击加入购物车  type ==2;
     * 点击买        type==3;
     */
    private int type = 1;
    private String mOid;

    private ArrayList<Icons> mData;
    private MyAdapter<Icons> mAdapter;
    private RecyclerView mRecyclerView;
    private List<JudgeGroupStatusBean2.MemberBean> mMJudgeGroupBeanMember;
    private String mType;
    private TextView mMake_money;
    private TextView mYuan;
    private TextView mYuan2;
    private RecyclerView mRecyclerView2;
    private int mLack;
    private TextView mTvGroupStyle;
    private TextView mInformation;
    private TextView mTvGoodsInformation;
    private TextView mTvGroupProfit2;
    private LinearLayout mLlAddress;
    private LinearLayout mLlCompany;
    private LinearLayout mLlOrderNum;
    private LinearLayout mLl_profit;
    private TextView mTvSuccess;
    private TextView mTvInvite;
    private TextView mTvJoin2;
    private TextView mTvGroupPlace;
    private int mGroupnum;
    private ImageView mIvLine1;
    private ImageView mIvLine2;
    private VpSwipeRefreshLayout mVpSwipeRefreshLayout;
    private ImageView mIvLine3;
    private TextView mTvJoin3;
    private LinearLayout mLlLack1;
    private LinearLayout mLlLack2;
    private TextView mTvKuaidi;
    private TextView mTvExpressn;
    private TextView mTvExpressnCopy;
    private LinearLayout mLlGroupingTime;
    private ImageView mIvLine5;
    private LinearLayout mLlcode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_order_details);
        Translucent.setTranslucent(this);
        ShareSDK.initSDK(this, "1dc05ab16fd6c");
        initView();
        setListener();
        Intent intent = getIntent();
        mGoodid = intent.getStringExtra("goodid");
        mTeamid = intent.getStringExtra("teamid");
        mType = intent.getStringExtra("type");
        SharedPreferences.Editor editor = MyApp.loginMessage.edit();
        //  editor.putString("teamid", item.getTeamid());
        if (! mTeamid.equals("")) {
            editor.putString("mTtT", "1");
            editor.putString("flag","");
        }
        editor.apply();

        mModel = new JudgeGroupStatusImpl();
        mModel.getJudgeGroupStatusData(mGoodid, mTeamid, this);
        mGroupShareModel = new GroupShareImpl();

    }

    private void setListener() {
        mIvback.setOnClickListener(this);
        mTvJoin.setOnClickListener(this);
        mVpSwipeRefreshLayout.setOnRefreshListener(this);
        //复制订单号
    }

    private void initView() {
        mIvback = (ImageView) findViewById(R.id.iv_back);
        mIvThumb = (ImageView) findViewById(R.id.iv_thumb);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvGroupNum = (TextView) findViewById(R.id.tv_groupnum);
        mTvGroupsprice = (TextView) findViewById(R.id.tv_groupsprice);
        mTvGroupProfit = (TextView) findViewById(R.id.tv_group_profit);
        mTvJoin = (TextView) findViewById(R.id.tv_join);
        mIvOpenSuccess = (ImageView) findViewById(R.id.iv_open_sucess);
        mIvGrouping = (ImageView) findViewById(R.id.iv_grouping);
        mIvGroupSucess = (ImageView) findViewById(R.id.iv_group_sucess);
        mIvGroupError = (ImageView) findViewById(R.id.iv_group_error);
        mTvLack = (TextView) findViewById(R.id.tv_lack);
        mTvLack2 = (TextView) findViewById(R.id.tv_lack2);
        mTvHour = (TextView) findViewById(R.id.tv_hour);
        mTvMinute = (TextView) findViewById(R.id.tv_minute);
        mTvSecond = (TextView) findViewById(R.id.tv_second);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        mRecyclerView2 = (RecyclerView) findViewById(R.id.rv2);
        mTvGroupStyle = (TextView) findViewById(R.id.tv_group_style);
        mInformation = (TextView) findViewById(R.id.information);
        mTvGoodsInformation = (TextView) findViewById(R.id.tv_goods_information);
        mTvGroupProfit2 = (TextView) findViewById(R.id.tv_group_profit2);
        mLlAddress = (LinearLayout) findViewById(R.id.ll_address);
        mLlCompany = (LinearLayout) findViewById(R.id.ll_company);
        mLlOrderNum = (LinearLayout) findViewById(R.id.ll_ordernumber);
        mLl_profit = (LinearLayout) findViewById(R.id.ll_profit);
        mTvSuccess = (TextView) findViewById(R.id.tv_success);
        mTvInvite = (TextView) findViewById(R.id.tv_invite);
        mTvJoin2 = (TextView) findViewById(R.id.tv_join2);
        mTvGroupPlace = (TextView) findViewById(R.id.tv_group_place);
        mIvLine1 = (ImageView) findViewById(R.id.iv_line1);
        mIvLine2 = (ImageView) findViewById(R.id.iv_line2);
        mIvLine3 = (ImageView) findViewById(R.id.iv_line3);
        mIvLine5 = (ImageView) findViewById(R.id.iv_line5);

        mVpSwipeRefreshLayout = (VpSwipeRefreshLayout) findViewById(R.id.VpSwipeRefreshLayout);
        mTvJoin3 = (TextView) findViewById(R.id.tv_join3);
        mLlLack1 = (LinearLayout) findViewById(R.id.ll_lack1);
        mLlLack2 = (LinearLayout) findViewById(R.id.ll_lack2);
        mTvKuaidi = (TextView) findViewById(R.id.tv_group_kuaidi);
        mTvExpressn = (TextView) findViewById(R.id.tv_expressn);
        mTvExpressnCopy = (TextView) findViewById(R.id.tv_expressn_copy);
        mLlGroupingTime = (LinearLayout) findViewById(R.id.ll_grouping_time);
        mLlcode = (LinearLayout) findViewById(R.id.ll_codes);


    }

    @Override
    public void ongetJudgeGroupStatusDataSuccess(Object object) {
        mJudgeGroupBean = (JudgeGroupStatusBean2) object;
        showView();
        showTimeDate();
        if (timer == null) {
            timer = new Timer();
        } else {
            timer.cancel();
            timer = new Timer();
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg = handler.obtainMessage();
                handler.sendMessage(msg);
            }
        }, 0, 999);
        SharedPreferences.Editor editor = MyApp.loginMessage.edit();
        editor.putString("teamid", "");
        editor.putString("tt", "");
        editor.apply();

    }

    private void showTimeDate() {
        Long Now = System.currentTimeMillis() / 1000;
        long time = Long.parseLong(mJudgeGroupBean.getEndstr()) - Now;
        String[] strs = DateUtil.getDaoJiShiTimeUnixs(time).split(":");
        mTvHour.setText(NumberUtils.oneTtwo(strs[0]));
        mTvMinute.setText(NumberUtils.oneTtwo(strs[1]));
        mTvSecond.setText(NumberUtils.oneTtwo(strs[2]));
    }

    private void showView() {
        Picasso.with(context).load(mJudgeGroupBean.getThumb()).into(mIvThumb);
        mTvTitle.setText(mJudgeGroupBean.getTitle());
        mTvGroupNum.setText(mJudgeGroupBean.getGroupnum());
        mTvGroupsprice.setText(mJudgeGroupBean.getGroupsprice());
        mTvGroupProfit.setText(mJudgeGroupBean.getGroupsprice());
        if (MyApp.loginMessage.getString("isagent", "").equals("1")) {
            mLlAddress.setVisibility(View.VISIBLE);
            mTvGroupPlace.setText(mJudgeGroupBean.getAddress());
        }else {
            mLlAddress.setVisibility(View.GONE);
        }
        if (mJudgeGroupBean.getTt().equals("1")) {
            mLlcode.setVisibility(View.GONE);
        }
      /*  SharedPreferences.Editor editor = MyApp.loginMessage.edit();
        editor.putString("TT", mJudgeGroupBean.getTt());
        editor.apply();*/

        // 1开团成功 0开团中时间未到(开团进行中) -1时间到了开团失败
        if ("1".equals(mJudgeGroupBean.getGroupsuccess())) {             //  成功-------------
            mIvGroupSucess.setVisibility(View.VISIBLE);
            mIvOpenSuccess.setVisibility(View.GONE);
            mIvGrouping.setVisibility(View.GONE);
            mIvGroupError.setVisibility(View.GONE);
            mTvSuccess.setText("感谢天,感谢地,感谢你的一臂之力!");
            mTvLack.setVisibility(View.GONE);
            mTvInvite.setVisibility(View.GONE);
            mTvJoin2.setVisibility(View.VISIBLE);
            mTvJoin.setVisibility(View.GONE);
            mLlGroupingTime.setVisibility(View.GONE);
            mLlLack2.setVisibility(View.INVISIBLE);
            mTvJoin2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(GroupOrderDetailsActivity.this, GroupActivity.class);
                    startActivity(intent);
                }
            });

            if ("1".equals(mJudgeGroupBean.getType())) {
            //    LogUtils.e("TYPE", mJudgeGroupBean.getType());
                mTvGroupStyle.setText(mJudgeGroupBean.getOid());
                mInformation.setText("利润信息:");
                //订单状态 0 未支付  1已支付 2待收货 3已完成 -1取消  1付款。2 代收货  3已完成
                if ("0".equals(mJudgeGroupBean.getSendprofit())) {
                    mTvGoodsInformation.setText("待发放");
                } else if ("1".equals(mJudgeGroupBean.getSendprofit())) {
                    mTvGoodsInformation.setText("已发放");
                }
                mTvGroupProfit2.setText(mJudgeGroupBean.getGroupsprice());

                mLlCompany.setVisibility(View.GONE);
                mLlOrderNum.setVisibility(View.GONE);
                mIvLine1.setVisibility(View.GONE);
                mIvLine2.setVisibility(View.GONE);
                mIvLine3.setVisibility(View.GONE);

            } else if ("2".equals(mJudgeGroupBean.getType())) {
                mTvGroupStyle.setText(mJudgeGroupBean.getOid());
                mInformation.setText("商品信息:");
                mLl_profit.setVisibility(View.GONE);
                mIvLine1.setVisibility(View.VISIBLE);
                mIvLine5.setVisibility(View.GONE);
                if ("0".equals(mJudgeGroupBean.getOrderStatus())) {
                    mTvGoodsInformation.setText("未支付");
                } else if ("1".equals(mJudgeGroupBean.getOrderStatus())) {
                    mTvGoodsInformation.setText("已支付");
                } else if ("2".equals(mJudgeGroupBean.getOrderStatus())) {
                    mTvGoodsInformation.setText("待收货");
                } else if ("3".equals(mJudgeGroupBean.getOrderStatus())) {
                    mTvGoodsInformation.setText("已完成");
                } else if ("-1".equals(mJudgeGroupBean.getOrderStatus())) {
                    mTvGoodsInformation.setText("取消");
                }


                if ("2".equals(mJudgeGroupBean.getOrderStatus())||"3".equals(mJudgeGroupBean.getOrderStatus())) {
                    if (ASUtils.isCanUse(mJudgeGroupBean.getExpress())) {
                        mTvKuaidi.setText(mJudgeGroupBean.getExpress());
                    }
                    if (ASUtils.isCanUse(mJudgeGroupBean.getExpresssn())) {
                        mTvExpressn.setText(mJudgeGroupBean.getExpresssn());
                    }
                }else {
                    mLlCompany.setVisibility(View.GONE);
                    mLlOrderNum.setVisibility(View.GONE);
                    mIvLine5.setVisibility(View.GONE);
                    mIvLine3.setVisibility(View.GONE);
                }

                mTvExpressnCopy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (ASUtils.isCanUse(mJudgeGroupBean.getExpresssn())) {
                            ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, mJudgeGroupBean.getExpresssn());
                        }
                    }
                });

            }

        } else if ("0".equals(mJudgeGroupBean.getGroupsuccess())) {        //  进行中----------------------
            mIvGrouping.setVisibility(View.VISIBLE);
            mIvOpenSuccess.setVisibility(View.GONE);
            mIvGroupSucess.setVisibility(View.GONE);
            mIvGroupError.setVisibility(View.GONE);

            // mIv.setVisibility(View.VISIBLE);
            if ("1".equals(mJudgeGroupBean.getType())) {
                mTvGroupStyle.setText("拿利润");
                mInformation.setText("利润信息:");
                //订单状态 0 未支付  1已支付 2待收货 3已完成 -1取消  1付款。2 代收货  3已完成

                if ("0".equals(mJudgeGroupBean.getSendprofit())) {
                    mTvGoodsInformation.setText("待发放");
                } else if ("1".equals(mJudgeGroupBean.getSendprofit())) {
                    mTvGoodsInformation.setText("已发放");
                }
                mTvGroupProfit2.setText(mJudgeGroupBean.getProfit());
                mLlAddress.setVisibility(View.VISIBLE);
                mLlCompany.setVisibility(View.GONE);
                mLlOrderNum.setVisibility(View.GONE);
                mIvLine1.setVisibility(View.GONE);
                mIvLine2.setVisibility(View.GONE);
                mIvLine3.setVisibility(View.GONE);

                //   mTvGroupPlace
            } else if ("2".equals(mJudgeGroupBean.getType())) {
                mTvGroupStyle.setText(mJudgeGroupBean.getOid());
                mInformation.setText("商品信息:");
                // 订单状态:0 未支付  1已支付 2待收货 3已完成
                mTvGoodsInformation.setText("待成团");
                mLlCompany.setVisibility(View.GONE);
                mLlOrderNum.setVisibility(View.GONE);
                mLl_profit.setVisibility(View.GONE);
                mIvLine1.setVisibility(View.VISIBLE);
                mIvLine2.setVisibility(View.VISIBLE);
                mIvLine3.setVisibility(View.GONE);


            }
        } else if ("-1".equals(mJudgeGroupBean.getGroupsuccess())) {          //失败------------------
            mIvGroupError.setVisibility(View.VISIBLE);
            mIvOpenSuccess.setVisibility(View.GONE);
            mIvGroupSucess.setVisibility(View.GONE);
            mIvGrouping.setVisibility(View.GONE);
            mTvGroupStyle.setText("拿利润");
            mInformation.setText("利润信息:");
            mTvGoodsInformation.setText("发放失败");
            mLlAddress.setVisibility(View.GONE);
            mLlCompany.setVisibility(View.GONE);
            mLlOrderNum.setVisibility(View.GONE);
            mLl_profit.setVisibility(View.GONE);
            mIvLine1.setVisibility(View.GONE);
            mIvLine2.setVisibility(View.GONE);
            mIvLine3.setVisibility(View.GONE);
            mTvJoin3.setVisibility(View.VISIBLE);
            mTvJoin.setVisibility(View.GONE);
            mTvJoin2.setVisibility(View.GONE);
            mLlLack1.setVisibility(View.GONE);
            mIvLine5.setVisibility(View.GONE);
            mLlLack2.setVisibility(View.INVISIBLE);
            mLlGroupingTime.setVisibility(View.GONE);
            mTvJoin3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(GroupOrderDetailsActivity.this, GroupActivity.class);
                    startActivity(intent);


                }
            });
        }
        mGroupnum = Integer.parseInt(mJudgeGroupBean.getGroupnum());
        mLack = mGroupnum - mJudgeGroupBean.getMember().size();
        mTvLack.setText(mLack + "");
        mTvLack2.setText(mLack + "");
        mOid = mJudgeGroupBean.getOid();
        mMJudgeGroupBeanMember = mJudgeGroupBean.getMember();

        final List<JudgeGroupStatusBean2.MemberBean> MemberBeans = new ArrayList<>();
        for (int i = 0; i < mMJudgeGroupBeanMember.size(); i++) {
            MemberBeans.add(mMJudgeGroupBeanMember.get(i));
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView2.setLayoutManager(linearLayoutManager);
        mRecyclerView2.setAdapter(new QuickAdapter<JudgeGroupStatusBean2.MemberBean>(MemberBeans) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.recyclerview_item_order_detail2;
            }

            @Override
            public void convert(VH holder, final JudgeGroupStatusBean2.MemberBean data, int position) {
                ImageView view4 = holder.getView(R.id.iv_line4);
                int pos = position + 1;
                holder.setText(R.id.tv_usename, data.getNickname());
                holder.setText(R.id.tv_join_time, data.getCreatetime());
                //"1".equals(data.getHeads())
                if (position == 0) {
                    TextView view1 = holder.getView(R.id.tv_usename);
                    view1.setTextColor(getResources().getColor(R.color.all_white));
                    TextView view2 = holder.getView(R.id.tv_join_time);
                    view2.setTextColor(getResources().getColor(R.color.all_white));
                    TextView view3 = holder.getView(R.id.tv_opengroup);
                    view3.setTextColor(getResources().getColor(R.color.all_white));
                    holder.getView(R.id.ll_item).setBackgroundColor(getResources().getColor(R.color.themeColor2));
                    holder.getView(R.id.tv_opengroup).setVisibility(View.VISIBLE);
                    holder.getView(R.id.tv_joingroup).setVisibility(View.GONE);
                    holder.getView(R.id.iv_label).setVisibility(View.GONE);
                    CircleImageView view = holder.getView(R.id.civ_my_photo);
                    Picasso.with(context).load(data.getAvatar()).into(view);
                    holder.getView(R.id.ll_order_information1).setVisibility(View.GONE);
                    holder.getView(R.id.ll_order_information2).setVisibility(View.GONE);
                    holder.getView(R.id.ll_order_information3).setVisibility(View.GONE);
                    holder.getView(R.id.ll_order_information4).setVisibility(View.GONE);
                    holder.getView(R.id.ll_order_information5).setVisibility(View.GONE);
                    holder.getView(R.id.ll_order_information6).setVisibility(View.GONE);
                } else if (position == 1) {
                    holder.getView(R.id.tv_opengroup).setVisibility(View.GONE);
                    holder.getView(R.id.tv_joingroup).setVisibility(View.VISIBLE);
                    holder.getView(R.id.iv_label).setVisibility(View.VISIBLE);
                    CircleImageView view = holder.getView(R.id.civ_my_photo);
                    if (!data.getAvatar().isEmpty()) {
                        Picasso.with(context).load(data.getAvatar()).into(view);
                    }

                    holder.getView(R.id.ll_order_information1).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information2).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information3).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information4).setVisibility(View.VISIBLE);
                    holder.setText(R.id.tv_orderno,data.getOrderno());
                    holder.getView(R.id.tv_orderno_copy1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ASUtils.isCanUse(data.getOrderno())) {
                                ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, data.getOrderno());
                            }
                        }
                    });
                    if ("".equals(data.getExpresscom())) {
                        holder.getView(R.id.ll_order_information2).setVisibility(View.GONE);
                    } else {
                        holder.setText(R.id.tv_expressncom,data.getExpresscom());
                    }
                    if ("".equals(data.getExpresssn())) {
                        holder.getView(R.id.ll_order_information3).setVisibility(View.GONE);

                    }else {
                        holder.setText(R.id.tv_expressn3,data.getExpresssn());
                    }

                    holder.getView(R.id.tv_expressn_copy3).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ASUtils.isCanUse(data.getExpresssn())) {
                                ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, data.getExpresssn());
                            }
                        }
                    });
                    holder.setText(R.id.tv_address4,data.getAddress());
                    //-1 已退款 1已付款 2待收货 3已完成

                    if ("3".equals(data.getOrderstatus())) {
                        holder.setText(R.id.order_state,"已完成");
                    } else if (data.getOrderstatus().equals("2")) {
                        holder.setText(R.id.order_state,"待收货");
                    }else if (data.getOrderstatus().equals("1")) {
                        holder.setText(R.id.order_state,"已付款");
                    }else if (data.getOrderstatus().equals("-1")) {
                        holder.setText(R.id.order_state,"已退款");
                    }
                    holder.setText(R.id.tv_user,data.getUsername()+"("+data.getMobile()+")");

                    //        LogUtils.e("position", position + "");
                } else if (pos == MemberBeans.size()) {
                    LogUtils.e("size:", MemberBeans.size() + "");
                    holder.getView(R.id.tv_opengroup).setVisibility(View.GONE);
                    holder.getView(R.id.tv_joingroup).setVisibility(View.VISIBLE);
                    holder.getView(R.id.iv_label).setVisibility(View.INVISIBLE);
                    CircleImageView view = holder.getView(R.id.civ_my_photo);
                    Picasso.with(context).load(data.getAvatar()).into(view);
                    view4.setVisibility(View.GONE);
                    holder.getView(R.id.ll_order_information1).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information2).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information3).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information4).setVisibility(View.VISIBLE);
                    holder.setText(R.id.tv_orderno,data.getOrderno());
                    holder.getView(R.id.tv_orderno_copy1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ASUtils.isCanUse(data.getOrderno())) {
                                ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, data.getOrderno());
                            }
                        }
                    });
                 if (data.getExpresscom().isEmpty()) {
                        holder.getView(R.id.ll_order_information2).setVisibility(View.GONE);
                    } else {
                        holder.setText(R.id.tv_expressncom,data.getExpresscom());
                    }
                    if (data.getExpresssn().isEmpty()) {
                        holder.getView(R.id.ll_order_information3).setVisibility(View.GONE);

                    }else {
                        holder.setText(R.id.tv_expressn3,data.getExpresssn());
                    }

                    holder.getView(R.id.tv_expressn_copy3).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ASUtils.isCanUse(data.getExpresssn())) {
                                ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, data.getExpresssn());
                            }
                        }
                    });
                    holder.setText(R.id.tv_address4,data.getAddress());
                    if (data.getOrderstatus().equals("3")) {
                        holder.setText(R.id.order_state,"已完成");
                    } else if (data.getOrderstatus().equals("2")) {
                        holder.setText(R.id.order_state,"待收货");
                    }else if (data.getOrderstatus().equals("1")) {
                        holder.setText(R.id.order_state,"已付款");
                    }else if (data.getOrderstatus().equals("-1")) {
                        holder.setText(R.id.order_state,"已退款");
                    }
                    holder.setText(R.id.tv_user,data.getUsername()+"("+data.getMobile()+")");
                 //   LogUtils.e("address:",data.getAddress());
                } else {
                    // LogUtils.e("position1:",pos+"");
                    //  LogUtils.e("size:",MemberBeans.size()+"");
                    holder.getView(R.id.tv_opengroup).setVisibility(View.GONE);
                    holder.getView(R.id.tv_joingroup).setVisibility(View.VISIBLE);
                    holder.getView(R.id.iv_label).setVisibility(View.INVISIBLE);
                    CircleImageView view = holder.getView(R.id.civ_my_photo);
                    Picasso.with(context).load(data.getAvatar()).into(view);
                    holder.getView(R.id.ll_order_information1).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information2).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information3).setVisibility(View.VISIBLE);
                    holder.getView(R.id.ll_order_information4).setVisibility(View.VISIBLE);
                    holder.setText(R.id.tv_orderno,data.getOrderno());
                    holder.getView(R.id.tv_orderno_copy1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ASUtils.isCanUse(data.getOrderno())) {
                                ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, data.getOrderno());
                            }
                        }
                    });
                    if (data.getExpresscom().isEmpty()) {
                        holder.getView(R.id.ll_order_information2).setVisibility(View.GONE);
                    } else {
                        holder.setText(R.id.tv_expressncom,data.getExpresscom());
                    }
                    if (data.getExpresssn().isEmpty()) {
                        holder.getView(R.id.ll_order_information3).setVisibility(View.GONE);

                    }else {
                        holder.setText(R.id.tv_expressn3,data.getExpresssn());
                    }

                    holder.getView(R.id.tv_expressn_copy3).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ASUtils.isCanUse(data.getExpresssn())) {
                                ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, data.getExpresssn());
                            }
                        }
                    });

                    holder.setText(R.id.tv_address4,data.getAddress());
                  //  LogUtils.e("address:",data.getAddress());
                    if (data.getOrderstatus().equals("3")) {
                        holder.setText(R.id.order_state,"已完成");
                    } else if (data.getOrderstatus().equals("2")) {
                        holder.setText(R.id.order_state,"待收货");
                    }else if (data.getOrderstatus().equals("1")) {
                        holder.setText(R.id.order_state,"已付款");
                    }else if (data.getOrderstatus().equals("-1")) {
                        holder.setText(R.id.order_state,"已退款");
                    }
                    holder.setText(R.id.tv_user,data.getUsername()+"("+data.getMobile()+")");
                }

            }
        });

        List<AvatarData> Avatars = new ArrayList<>();
        for (int i = 0; i < mMJudgeGroupBeanMember.size(); i++) {
            AvatarData avatar = new AvatarData();
            avatar.setUrl(mMJudgeGroupBeanMember.get(i).getAvatar());
            Avatars.add(avatar);
        }

        for (int i = mMJudgeGroupBeanMember.size(); i < mGroupnum; i++) {
            AvatarData avatar2 = new AvatarData();
            avatar2.setUrl("www" + i);
            Avatars.add(avatar2);
        }

  /*      LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);*/
        if (mGroupnum == 2) {
            mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        } else if (mGroupnum == 3) {
            mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        } else {
            mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        }

        mRecyclerView.setAdapter(new QuickAdapter<AvatarData>(Avatars) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.rv_item2;
            }

            @Override
            public void convert(VH holder, AvatarData data, int position) {
                ImageView view1 = holder.getView(R.id.iv_tuanzhang);
                CircleImageView view = holder.getView(R.id.civ);
                //   ImageView view1 = holder.getView(R.id.iv_tuanzhang);
                ImageView view2 = holder.getView(R.id.iv_shafa);
                if (position == 0) {
                    view1.setVisibility(View.VISIBLE);
                    Picasso.with(context).load(data.getUrl()).into(view);
                } else if (position == 1 && mMJudgeGroupBeanMember.size() > 1) {
                    view1.setVisibility(View.GONE);
                    if (!data.getUrl().isEmpty()) {
                        Picasso.with(context).load(data.getUrl()).into(view);
                    }
                    view2.setVisibility(View.VISIBLE);
                } else {
                    if (position == mMJudgeGroupBeanMember.size()) {
                  /*  if ("http://img.banzhishenghuo.com/images/3/2018/05/qNDSF4TMsooSfs4DK9S1O5145Stt9N.png".equals(data.getUrl())) {
                        Picasso.with(context).load(R.mipmap.default_image1).into(view);
                    }*/
                        view1.setVisibility(View.GONE);
                        view2.setVisibility(View.GONE);
                        //   view2.setVisibility(View.VISIBLE);
                        //    CircleImageView view = holder.getView(R.id.civ);
                        Picasso.with(context).load(R.mipmap.default_image1).into(view);
                    } else if (data.getUrl().startsWith("www")) {
                        Picasso.with(context).load(R.mipmap.default_image1).into(view);

                    } else {
                        view1.setVisibility(View.GONE);
                        view2.setVisibility(View.GONE);
                        //CircleImageView view = holder.getView(R.id.civ);
                        Picasso.with(context).load(data.getUrl()).into(view);
                    }
                }
            }

        });
    }

    @Override
    public void ongetJudgeGroupStatusDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
       // mVpSwipeRefreshLayout.setVisibility(View.GONE);
        mTvInvite.setVisibility(View.GONE);
        mTvJoin2.setVisibility(View.GONE);
        mTvJoin3.setVisibility(View.GONE);
        mTvJoin.setVisibility(View.GONE);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
             //   finish();
                Intent intent = new Intent(GroupOrderDetailsActivity.this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_join:
                showSharePopupwindow();
                break;
        }
    }

    private void showSharePopupwindow() {
        View contentView = LayoutInflater.from(GroupOrderDetailsActivity.this).inflate(R.layout.popupwindow_share_goods, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        ImageView ivchacha = (ImageView) contentView.findViewById(R.id.iv_chacha);
        TextView tvprice = (TextView) contentView.findViewById(R.id.tvmoney);
        TextView tvpricetwo = (TextView) contentView.findViewById(R.id.tv_money_two);
        LinearLayout llpengyouquan = (LinearLayout) contentView.findViewById(R.id.ll_pengyouquan);
        LinearLayout llweixin = (LinearLayout) contentView.findViewById(R.id.ll_weixing);
        LinearLayout lllianjie = (LinearLayout) contentView.findViewById(R.id.ll_lianjie);
        LinearLayout llerweima = (LinearLayout) contentView.findViewById(R.id.ll_erweima);
        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        mMake_money = (TextView) contentView.findViewById(R.id.make_money);
        mYuan = (TextView) contentView.findViewById(R.id.yuan);
        mYuan2 = (TextView) contentView.findViewById(R.id.yuan2);
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        if (mJudgeGroupBean != null) {
            if ("1".equals(mJudgeGroupBean.getType())) {
                tvprice.setText(NumberUtils.DecimalFormat(mJudgeGroupBean.getGroupsprice()));
                tvpricetwo.setText(NumberUtils.DecimalFormat(mJudgeGroupBean.getGroupsprice()));
            } else {
                mMake_money.setVisibility(View.GONE);
                tvprice.setText("免费得商品");
                tvpricetwo.setText("免费得到此商品");
                mYuan.setVisibility(View.GONE);
                mYuan2.setVisibility(View.GONE);
            }
            lllianjie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(GroupOrderDetailsActivity.this, "正在加载,请稍后~");
                        //  wxmodel.getData(goodsdata.getId(), null, LIANJIE, GroupOrderDetailsActivity.this);
                        mGroupShareModel.getGroupShareData(mOid, LIANJIE, GroupOrderDetailsActivity.this);
                    } else {
                        ShareUtils.copyLianjie(GroupOrderDetailsActivity.this, wxURL);
                    }
                }
            });
            llerweima.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(GroupOrderDetailsActivity.this, "正在加载,请稍后~");
                        // wxmodel.getData(goodsdata.getId(), null, ERWEIMA, GroupOrderDetailsActivity.this);
                        mGroupShareModel.getGroupShareData(mOid, ERWEIMA, GroupOrderDetailsActivity.this);
                    } else {
                        ShareUtils.ereima4(GroupOrderDetailsActivity.this, mJudgeGroupBean.getTitle(), mJudgeGroupBean.getGroupsprice(), null, null, null, mJudgeGroupBean.getThumb(), wxURL);
                    }
                }
            });
            llweixin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        //  type = 1;
                        LogUtils.e("步骤", "1");
                        LoadingDialogUtils.showProgress(GroupOrderDetailsActivity.this, "正在加载,请稍后~");
                        //  wxmodel.getData(goodsdata.getId(), null, WEIXINHAOYOU, GroupOrderDetailsActivity.this);
                        mGroupShareModel.getGroupShareData(mOid, WEIXINHAOYOU, GroupOrderDetailsActivity.this);
                    } else {
                        LogUtils.e("步骤", "2" + "     title-->" + mGroupShareBean.getSharetitle() + "    link-->" + mGroupShareBean.getSharelink() + "   wxURL-->" + wxURL);
                        ShareUtils.wxshare(mGroupShareBean.getSharetitle(), mGroupShareBean.getShareicon(), mGroupShareBean.getSharedesc(), wxURL);
                        //  ShareUtils.wxshare(mJudgeGroupBean.getTitle(), mJudgeGroupBean.getThumb(), mGroupShareBean.getSharedesc(), wxURL);
                    }
                }
            });
            llpengyouquan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (wxURL == null) {
                        LoadingDialogUtils.showProgress(GroupOrderDetailsActivity.this, "正在加载,请稍后~");
                        //   wxmodel.getData(goodsdata.getId(), null, PENGYOUQUAN, GroupOrderDetailsActivity.this);
                        mGroupShareModel.getGroupShareData(mOid, PENGYOUQUAN, GroupOrderDetailsActivity.this);
                    } else {
                        ShareUtils.wxPengyouquan(mJudgeGroupBean.getTitle(), mJudgeGroupBean.getThumb(), wxURL);
                    }
                }
            });
        }
        ivchacha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxURL = null;
                popWindow.dismiss();
            }
        });
        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }

    @Override
    public void ongetGroupShareDataSuccess(Object object, int num) {
        LoadingDialogUtils.dismiss();
        mGroupShareBean = (GroupShareBean) object;
        wxURL = mGroupShareBean.getSharelink();
        if (num == LIANJIE) {
            ShareUtils.copyLianjie(this, wxURL);
        } else if (num == ERWEIMA) {
            ShareUtils.ereima4(this, mGroupShareBean.getSharetitle(), mGroupShareBean.getGroupsprice(), null, null, null, mGroupShareBean.getShareicon(), wxURL);
        } else if (num == WEIXINHAOYOU) {
            //    LogUtils.e("步骤","3"+"     title-->"+mGroupShareBean.getSharetitle()+"    link-->"+mGroupShareBean.getSharelink()+"   wxURL-->"+wxURL);
            ShareUtils.wxshare(mGroupShareBean.getSharetitle(), mGroupShareBean.getShareicon(), mGroupShareBean.getSharedesc(), wxURL);
            //   ShareUtils.wxshare(mJudgeGroupBean.getTitle(), mJudgeGroupBean.getThumb(), mGroupShareBean.getSharedesc(), wxURL);
        } else if (num == PENGYOUQUAN) {
            ShareUtils.wxPengyouquan(mGroupShareBean.getSharetitle(), mGroupShareBean.getShareicon(), wxURL);
        }
    }

    @Override
    public void ongetGroupShareDataError(Object object, int num) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }

    @Override
    public void onRefresh() {
        mModel.getJudgeGroupStatusData(mGoodid, mTeamid, this);
        mVpSwipeRefreshLayout.setRefreshing(false);
    }
}
